/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.modelo.ApenasLetrasMaiusculaseMinuscula;
import br.com.evl.tecinfo.modelo.ApenasLetrasMinusculas;
import br.com.evl.tecinfo.modelo.MaiusculaseMinusculaseEspaco;
import java.awt.Color;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author D_Y
 */
public class EditarUsuario extends javax.swing.JFrame {

    private int clicou_ok;
    int selected_Row = -1;
    private int linha_editar = -1;

    /**
     * Creates new form editarUsuario
     */
    public EditarUsuario() {
        initComponents();
        setSize(775, 550);
        setTitle("Editar Usuários");
        setLocationRelativeTo(this);

        //IMAGEM ÍCONE
        ImageIcon icone = new ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/icon64x64.png"));
        setIconImage(icone.getImage());

        //LIMITANDO CARACTERES DO FORMULÁRIO
        JTextField textFieldNome = new JTextField();
        jFormattedTextField_Nome.setDocument(new MaiusculaseMinusculaseEspaco(50));

        JTextField textFieldLogin = new JTextField();
        jFormattedTextField_Login.setDocument(new ApenasLetrasMinusculas(20));


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelUsuario = new javax.swing.JLabel();
        jButtonSalvarUsuario = new javax.swing.JButton();
        jButtonRemoverUsuario = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableUsuario = new javax.swing.JTable();
        jButtonEditUsuario = new javax.swing.JButton();
        jButtonSairUsuario = new javax.swing.JButton();
        jLabelIMGListadeUsuarios = new javax.swing.JLabel();
        jLabelIMGLogolistadefuncionarios = new javax.swing.JLabel();
        jLabel_Permissao = new javax.swing.JLabel();
        jLabel_Senha = new javax.swing.JLabel();
        jPasswordField_Senha = new javax.swing.JPasswordField();
        jLabel_Login = new javax.swing.JLabel();
        jFormattedTextField_Login = new javax.swing.JFormattedTextField();
        jLabel_Nome = new javax.swing.JLabel();
        jComboBox_Cargo = new javax.swing.JComboBox();
        jFormattedTextField_Nome = new javax.swing.JFormattedTextField();
        jLabel_IMG_bgBaixo = new javax.swing.JLabel();
        jLabel_IMG_bgLado = new javax.swing.JLabel();
        jLabel_IMG_bgCima = new javax.swing.JLabel();
        jLabel_IMG_bgLado2 = new javax.swing.JLabel();
        jLabel_IMG_bg2 = new javax.swing.JLabel();
        jLabelIMGBg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(null);

        jLabelUsuario.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabelUsuario.setForeground(new java.awt.Color(102, 102, 102));
        jLabelUsuario.setText("LISTA DE USUÁRIOS");
        getContentPane().add(jLabelUsuario);
        jLabelUsuario.setBounds(330, 30, 150, 40);

        jButtonSalvarUsuario.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jButtonSalvarUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/salvar20x20.png"))); // NOI18N
        jButtonSalvarUsuario.setText("SALVAR");
        jButtonSalvarUsuario.setEnabled(false);
        jButtonSalvarUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonSalvarUsuario);
        jButtonSalvarUsuario.setBounds(630, 200, 120, 30);

        jButtonRemoverUsuario.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jButtonRemoverUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/deletarusuario20x20.png"))); // NOI18N
        jButtonRemoverUsuario.setText("REMOVER");
        jButtonRemoverUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoverUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonRemoverUsuario);
        jButtonRemoverUsuario.setBounds(630, 160, 119, 30);

        jTableUsuario.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jTableUsuario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Login", "Senha", "Permissão"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTableUsuario);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(20, 330, 730, 170);

        jButtonEditUsuario.setFont(new java.awt.Font("Century Gothic", 0, 12)); // NOI18N
        jButtonEditUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/editarrelatorio20x20.png"))); // NOI18N
        jButtonEditUsuario.setText("EDITAR");
        jButtonEditUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonEditUsuario);
        jButtonEditUsuario.setBounds(630, 120, 120, 30);

        jButtonSairUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/sair20x20.png"))); // NOI18N
        jButtonSairUsuario.setText("SAIR");
        jButtonSairUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSairUsuarioActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonSairUsuario);
        jButtonSairUsuario.setBounds(630, 240, 120, 30);

        jLabelIMGListadeUsuarios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/CADASTRODEUSUARIO.png"))); // NOI18N
        getContentPane().add(jLabelIMGListadeUsuarios);
        jLabelIMGListadeUsuarios.setBounds(300, 30, 210, 40);

        jLabelIMGLogolistadefuncionarios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/login.png"))); // NOI18N
        getContentPane().add(jLabelIMGLogolistadefuncionarios);
        jLabelIMGLogolistadefuncionarios.setBounds(40, 20, 70, 80);

        jLabel_Permissao.setText("Permissão:");
        jLabel_Permissao.setEnabled(false);
        getContentPane().add(jLabel_Permissao);
        jLabel_Permissao.setBounds(20, 220, 70, 30);

        jLabel_Senha.setText("Senha:");
        jLabel_Senha.setEnabled(false);
        getContentPane().add(jLabel_Senha);
        jLabel_Senha.setBounds(310, 180, 50, 30);

        jPasswordField_Senha.setEnabled(false);
        getContentPane().add(jPasswordField_Senha);
        jPasswordField_Senha.setBounds(360, 180, 230, 30);

        jLabel_Login.setText("Login:");
        jLabel_Login.setEnabled(false);
        getContentPane().add(jLabel_Login);
        jLabel_Login.setBounds(20, 180, 60, 30);

        jFormattedTextField_Login.setEnabled(false);
        getContentPane().add(jFormattedTextField_Login);
        jFormattedTextField_Login.setBounds(90, 180, 210, 30);

        jLabel_Nome.setText("Nome:");
        jLabel_Nome.setEnabled(false);
        getContentPane().add(jLabel_Nome);
        jLabel_Nome.setBounds(20, 140, 50, 30);

        jComboBox_Cargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "nenhum", "Assitente", "Coordenador", "Presidente", "Professor" }));
        jComboBox_Cargo.setEnabled(false);
        getContentPane().add(jComboBox_Cargo);
        jComboBox_Cargo.setBounds(90, 220, 210, 30);

        jFormattedTextField_Nome.setEnabled(false);
        getContentPane().add(jFormattedTextField_Nome);
        jFormattedTextField_Nome.setBounds(90, 140, 500, 30);

        jLabel_IMG_bgBaixo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/backgroudlado - CIMA - Copia.jpg"))); // NOI18N
        getContentPane().add(jLabel_IMG_bgBaixo);
        jLabel_IMG_bgBaixo.setBounds(0, 270, 760, 10);

        jLabel_IMG_bgLado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/abamenu.jpg"))); // NOI18N
        getContentPane().add(jLabel_IMG_bgLado);
        jLabel_IMG_bgLado.setBounds(0, 110, 10, 170);

        jLabel_IMG_bgCima.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/backgroudlado - CIMA.jpg"))); // NOI18N
        getContentPane().add(jLabel_IMG_bgCima);
        jLabel_IMG_bgCima.setBounds(0, 110, 760, 10);

        jLabel_IMG_bgLado2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/abamenu.jpg"))); // NOI18N
        getContentPane().add(jLabel_IMG_bgLado2);
        jLabel_IMG_bgLado2.setBounds(610, 110, 160, 170);

        jLabel_IMG_bg2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/registro.jpg"))); // NOI18N
        getContentPane().add(jLabel_IMG_bg2);
        jLabel_IMG_bg2.setBounds(0, 110, 770, 170);

        jLabelIMGBg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/evl/tecinfo/gui/imagens/backgroud.jpg"))); // NOI18N
        getContentPane().add(jLabelIMGBg);
        jLabelIMGBg.setBounds(0, 0, 770, 530);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSairUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSairUsuarioActionPerformed
        clicou_ok = JOptionPane.showConfirmDialog(null, "Deseja cancelar o cadastramento da área?", "Confirmação", JOptionPane.YES_NO_OPTION);
        if (clicou_ok == JOptionPane.YES_OPTION) {
            this.dispose();
        } else if (clicou_ok == JOptionPane.NO_OPTION) {
        }
    }//GEN-LAST:event_jButtonSairUsuarioActionPerformed

    private void jButtonEditUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditUsuarioActionPerformed
        // CONDIÇÃO PARA SÓ ABLITAR QUANDO ALGUMA COLUA ESTIVER SELECIONADA 
        //  if (jTableUsuario.getSelectedRowCount() != 0) {
        jLabel_Nome.setEnabled(true);
        jLabel_Login.setEnabled(true);
        jLabel_Permissao.setEnabled(true);
        jLabel_Senha.setEnabled(true);
        jFormattedTextField_Login.setEnabled(true);
        jFormattedTextField_Nome.setEnabled(true);
        jPasswordField_Senha.setEnabled(true);
        jComboBox_Cargo.setEnabled(true);
        jButtonSalvarUsuario.setEnabled(true);
        jButtonEditUsuario.setEnabled(false);
        jButtonRemoverUsuario.setEnabled(false);
        //  } else {            
        //     java.awt.Toolkit.getDefaultToolkit().beep();
        //     JOptionPane.showMessageDialog(null,"Por favor selecione uma linha da tabela");
        //  }
    }//GEN-LAST:event_jButtonEditUsuarioActionPerformed

    private void jButtonSalvarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarUsuarioActionPerformed
        // TODO add your handling code here:

        //jFormattedTextField_Nome.getText(jTableUsuario.setRowSelectionInterval(selected_Row, 0));
        //jFormattedTextField_Login.getText(jTableUsuario.setRowSelectionInterval(selected_Row, 1));
        //jPasswordField_Senha.getText(jTableUsuario.setRowSelectionInterval(selected_Row, 2));
        //PERMISSÃO NÃO SEI COMO FAZER 
        //jFormattedTextField_Nome.getText(jTableUsuario.setRowSelectionInterval(selected_Row, 0));

        boolean nomevazio = false;
        boolean areavazio = false;
        boolean loginavazio = false;
        boolean senhavazio = false;
        boolean permissaovazio = false;
        //CÓDIGO PARA CHECAR SE OS CAMPOS DO CADASTROS E ALTERAR COR DOS CAMPOS VAZIOS
        if (linha_editar == -1) {
            if (jFormattedTextField_Nome.getText().equals("")) {
                jFormattedTextField_Nome.setBackground(new Color(255, 209, 209));
                nomevazio = true;
            } else {
                jFormattedTextField_Nome.setBackground(new Color(255, 255, 255));
                nomevazio = false;
            }
            if (jFormattedTextField_Login.getText().equals("")) {
                jFormattedTextField_Login.setBackground(new Color(255, 209, 209));
                loginavazio = true;
            } else {
                jFormattedTextField_Login.setBackground(new Color(255, 255, 255));
                loginavazio = false;
            }
            if (jPasswordField_Senha.getPassword().length == 0) {
                jPasswordField_Senha.setBackground(new Color(255, 209, 209));
                senhavazio = true;
            } else {
                jPasswordField_Senha.setBackground(new Color(255, 255, 255));
                senhavazio = false;
            }
            if (jComboBox_Cargo.getSelectedIndex() == 0) {
                jComboBox_Cargo.setBackground(new Color(255, 209, 209));
                permissaovazio = true;
            } else {
                jComboBox_Cargo.setBackground(new Color(255, 255, 255));
                permissaovazio = false;
            } //FIM DO CÓDICO DE CHECAR E ALTERAR COR DOS CAMPOS VAZIOS **********


            //LOGICA PARA MANDA MENSAGEM DE ERRO (EXISTE CAMPOS VAZIOS)
            if (nomevazio == true || areavazio == true || loginavazio == true || senhavazio == true || permissaovazio == true) {
                java.awt.Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(null, "Por favor preencha os campos vazios.");
            } else if (nomevazio == false && areavazio == false && loginavazio == false && senhavazio == false
                    && permissaovazio == false) {
                jFormattedTextField_Nome.setText("");
                jFormattedTextField_Login.setText("");
                jPasswordField_Senha.setText("");
                jComboBox_Cargo.setSelectedIndex(0);
                jButtonEditUsuario.setEnabled(true);
                jButtonRemoverUsuario.setEnabled(true);
                jButtonSalvarUsuario.setEnabled(false);
                jLabel_Nome.setEnabled(false);
                jLabel_Login.setEnabled(false);
                jLabel_Permissao.setEnabled(false);
                jLabel_Senha.setEnabled(false);
                jFormattedTextField_Login.setEnabled(false);
                jFormattedTextField_Nome.setEnabled(false);
                jPasswordField_Senha.setEnabled(false);
                jComboBox_Cargo.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jButtonSalvarUsuarioActionPerformed

    private void jButtonRemoverUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoverUsuarioActionPerformed
        if (jTableUsuario.getSelectedRowCount() != 0) {
            clicou_ok = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja deletar esse Usuário?", "Confirmação", JOptionPane.YES_NO_OPTION);
            if (clicou_ok == JOptionPane.YES_OPTION) {
            } else if (clicou_ok == JOptionPane.NO_OPTION) {
            }
        } else {
            java.awt.Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(null, "Por favor selecione uma linha da tabela");
        }
    }//GEN-LAST:event_jButtonRemoverUsuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditarUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditUsuario;
    private javax.swing.JButton jButtonRemoverUsuario;
    private javax.swing.JButton jButtonSairUsuario;
    private javax.swing.JButton jButtonSalvarUsuario;
    private javax.swing.JComboBox jComboBox_Cargo;
    private javax.swing.JFormattedTextField jFormattedTextField_Login;
    private javax.swing.JFormattedTextField jFormattedTextField_Nome;
    private javax.swing.JLabel jLabelIMGBg;
    private javax.swing.JLabel jLabelIMGListadeUsuarios;
    private javax.swing.JLabel jLabelIMGLogolistadefuncionarios;
    private javax.swing.JLabel jLabelUsuario;
    private javax.swing.JLabel jLabel_IMG_bg2;
    private javax.swing.JLabel jLabel_IMG_bgBaixo;
    private javax.swing.JLabel jLabel_IMG_bgCima;
    private javax.swing.JLabel jLabel_IMG_bgLado;
    private javax.swing.JLabel jLabel_IMG_bgLado2;
    private javax.swing.JLabel jLabel_Login;
    private javax.swing.JLabel jLabel_Nome;
    private javax.swing.JLabel jLabel_Permissao;
    private javax.swing.JLabel jLabel_Senha;
    private javax.swing.JPasswordField jPasswordField_Senha;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableUsuario;
    // End of variables declaration//GEN-END:variables
}
